import re


from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from finallpro.items import FinallproItem,amazonItem

class AmazonSpider(CrawlSpider):
    name = 'amazon'
    allowed_domains = ['amazon.cn']
    start_urls = ['http://www.amazon.cn/']

    rules = (
        Rule(SgmlLinkExtractor(allow=(r'/\w{10}/')),callback='amazon',follow=True),
        Rule(SgmlLinkExtractor(allow=(r'/b/',r'/s/'))),
    )

    def parse_item(self, response):
        hxs = HtmlXPathSelector(response)
        i = FinallproItem()
        #i['domain_id'] = hxs.select('//input[@id="sid"]/@value').extract()
        #i['name'] = hxs.select('//div[@id="name"]').extract()
        #i['description'] = hxs.select('//div[@id="description"]').extract()
        return i
    def amazon(self,response):
        hxs=HtmlXPathSelector(response)
        i=amazonItem()
        title=hxs.select("//span[@id='btAsinTitle']/text()")
        if title:
            i['title']=title.extract()[0]
        else:
            i['title']='fuckdsjh'
        price=hxs.select("//span[@id='actualPriceValue']/b[@class='priceLarge']/text()")
        if price:
            price=price.extract()[0]
            i['price']=price
        else:
            i['price']=00000000000000000000
        img=hxs.select("//img[@id='prodImage']/@src")
        if img:
            i['img']=img.extract()[0]
        else:
            i['img']=00000000000000000000000
        url=response.url
        id=re.findall(r'/(\w{10})/',url)
        if id:
            i['id']=id[0]
        else:
            i['id']=00000000000000000
        return i
